//
//  ALPS_TSVoiceDViewController.m
//  Alps
//
//  Created by dllo on 16/6/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_TSVoiceDViewController.h"
#import "ALPS_TSearchAllDModel.h"
#import "ALPS_TSVoiceDTCell.h"
#import "ALPS_PlayerMainViewController.h"
#import "ALPS_TIDListModel.h"
@interface ALPS_TSVoiceDViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;
//相关度按钮
@property (nonatomic, strong) UIButton *relevanceButton;
//播放最多按钮
@property (nonatomic, strong) UIButton *playButton;
//最新上传按钮
@property (nonatomic, strong) UIButton *latestUploadButton;


//按钮颜色与加载判断
@property (nonatomic, assign) NSInteger judge;
//加载页面判断
@property (nonatomic, assign) NSInteger page;

//构造id数组
@property (nonatomic, strong) NSMutableArray *idArray;
@end

@implementation ALPS_TSVoiceDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    self.judge = 0;
    [self getView];
    self.idArray = [NSMutableArray array];
    [self getRelevanceData:self.searchText number:1];

}

#pragma mark -- 构造界面
- (void)getView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, ScreeHeight - 64 - 36) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    [self.tableView registerClass:[ALPS_TSVoiceDTCell class] forCellReuseIdentifier:@"ALPS_TSVoiceDTCell"];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreshData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadingData];
    }];

}

-(void)refreshData
{
    [self.dataSource removeAllObjects];
    [self.tableView reloadData];
    if (self.judge == 0) {
        [self getRelevanceData:self.searchText number:1];
    }
    else if (self.judge == 1)
    {
        [self getPlayData:self.searchText number:1];
    }
    else
    {
        [self getLatestUploadData:self.searchText number:1];
    }
    self.page = 1;
    
}


-(void)loadingData
{
    self.page++;
    if (self.judge == 0) {
        
        [self getRelevanceData:self.searchText number:self.page];
    }
    else if (self.judge == 1)
    {
        [self getPlayData:self.searchText number:self.page];
    }
    else
    {
        [self getLatestUploadData:self.searchText number:self.page];
    }
}


#pragma mark -- 相关度的数据请求
- (void)getRelevanceData:(NSString *)string number:(NSInteger) num
{
    [WSSNetTool GetNetByURL:[NSString stringWithFormat:@"http://search.ximalaya.com/front/v1?condition=relation&core=track&device=iPhone&kw=%@&live=true&page=%ld&paidFilter=false&rows=20", string, num] body:nil headers:nil response:0 success:^(id result) {
        NSDictionary *dic = [result objectForKey:@"response"];
        NSArray *array = [dic objectForKey:@"docs"];
        for (NSDictionary *dict in array) {
            ALPS_TSearchAllDModel *model = [[ALPS_TSearchAllDModel alloc] initWithDic:dict];
            [self.dataSource addObject:model];
            ALPS_TIDListModel *dataModel = [[ALPS_TIDListModel alloc] init];
            dataModel.trackId = [[dict objectForKey:@"id"] integerValue];
            dataModel.title = [dict objectForKey:@"title"];
            dataModel.coverLarge = [dict objectForKey:@"cover_path"];
            [self.idArray addObject:dataModel];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        //最大加载数
        NSString *max = [dic objectForKey:@"numFound"];
        NSInteger maxPage = [max integerValue];
        if (maxPage % 2 == 1) {
            maxPage = maxPage / 20 + 1;
        }
        else
        {
            maxPage = maxPage / 20;
        }
        if (self.page == maxPage) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }

        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark -- 播放次数最多的数据请求
- (void)getPlayData:(NSString *)string number:(NSInteger)num
{
    
    [WSSNetTool GetNetByURL:[NSString stringWithFormat:@"http://search.ximalaya.com/front/v1?condition=play&core=track&device=iPhone&kw=%@&live=true&page=%ld&paidFilter=false&rows=20", string, num] body:nil headers:nil response:0 success:^(id result) {
        NSDictionary *dic = [result objectForKey:@"response"];
        NSArray *array = [dic objectForKey:@"docs"];
        for (NSDictionary *dict in array) {
            ALPS_TSearchAllDModel *model = [[ALPS_TSearchAllDModel alloc] initWithDic:dict];
            [self.dataSource addObject:model];
            ALPS_TIDListModel *dataModel = [[ALPS_TIDListModel alloc] init];
            dataModel.trackId = [[dict objectForKey:@"id"] integerValue];
            dataModel.title = [dict objectForKey:@"title"];
            dataModel.coverLarge = [dict objectForKey:@"cover_path"];
            [self.idArray addObject:dataModel];

        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        //最大加载数
        NSString *max = [dic objectForKey:@"numFound"];
        NSInteger maxPage = [max integerValue];
        if (maxPage % 2 == 1) {
            maxPage = maxPage / 20 + 1;
        }
        else
        {
            maxPage = maxPage / 20;
        }
        if (self.page == maxPage) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }

        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
    
}

#pragma mark -- 最新上传的数据请求
- (void)getLatestUploadData:(NSString *)string number:(NSInteger)num
{
    [WSSNetTool GetNetByURL:[NSString stringWithFormat:@"http://search.ximalaya.com/front/v1?condition=recent&core=track&device=iPhone&kw=%@&live=true&page=%ld&paidFilter=false&rows=20", string, num] body:nil headers:nil response:0 success:^(id result) {
        NSDictionary *dic = [result objectForKey:@"response"];
        NSArray *array = [dic objectForKey:@"docs"];
        for (NSDictionary *dict in array) {
            ALPS_TSearchAllDModel *model = [[ALPS_TSearchAllDModel alloc] initWithDic:dict];
            [self.dataSource addObject:model];
            ALPS_TIDListModel *dataModel = [[ALPS_TIDListModel alloc] init];
            dataModel.trackId = [[dict objectForKey:@"id"] integerValue];
            dataModel.title = [dict objectForKey:@"title"];
            dataModel.coverLarge = [dict objectForKey:@"cover_path"];
            [self.idArray addObject:dataModel];
        }
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        //最大加载数
        NSString *max = [dic objectForKey:@"numFound"];
        NSInteger maxPage = [max integerValue];
        if (maxPage % 2 == 1) {
            maxPage = maxPage / 20 + 1;
        }
        else
        {
            maxPage = maxPage / 20;
        }
        if (self.page == maxPage) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }

        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * (ScreeHeight / 667);
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ALPS_TSVoiceDTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ALPS_TSVoiceDTCell"];
    cell.model = [self.dataSource objectAtIndex:indexPath.row];
    return cell;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, 40)];
    topView.backgroundColor = [UIColor colorWithRed:0.89 green:0.88 blue:0.88 alpha:1.00];
    
    self.relevanceButton = [[UIButton alloc] init];
    [self.relevanceButton setTitle:@"相关度" forState:UIControlStateNormal];
    self.relevanceButton.backgroundColor = [UIColor grayColor];
    self.relevanceButton.titleLabel.font = [UIFont systemFontOfSize:14];
    self.relevanceButton.layer.cornerRadius = 14;
    [self.relevanceButton addTarget:self action:@selector(didClickedRelevanceButton:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:self.relevanceButton];
    [self.relevanceButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView).offset(7);
        make.left.equalTo(topView).offset(20);
        make.height.equalTo(26);
        make.width.equalTo(70);
    }];
    
    self.playButton = [[UIButton alloc] init];
    [self.playButton setTitle:@"播放最多" forState:UIControlStateNormal];
    [self.playButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.playButton.titleLabel.font = [UIFont systemFontOfSize:14];
    self.playButton.layer.cornerRadius = 14;
    [self.playButton addTarget:self action:@selector(didClickedPlayButton:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:self.playButton];
    [self.playButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView).offset(7);
        make.left.equalTo(self.relevanceButton.right).offset(62.5);
        make.height.equalTo(26);
        make.width.equalTo(70);
    }];
    
    self.latestUploadButton = [[UIButton alloc] init];
    [self.latestUploadButton setTitle:@"最新上传" forState:UIControlStateNormal];
    [self.latestUploadButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.latestUploadButton.titleLabel.font = [UIFont systemFontOfSize:14];
    self.latestUploadButton.layer.cornerRadius = 14;
    [self.latestUploadButton addTarget:self action:@selector(didClickedLatestUploadButton:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:self.latestUploadButton];
    [self.latestUploadButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView).offset(7);
        make.right.equalTo(topView).offset(-20);
        make.height.equalTo(26);
        make.width.equalTo(70);
    }];
    
    
    if (self.judge == 1) {
        [self.playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.playButton.backgroundColor = [UIColor grayColor];
        [self.relevanceButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        self.relevanceButton.backgroundColor = [UIColor clearColor];
    }
    
    if (self.judge == 2) {
        [self.latestUploadButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.latestUploadButton.backgroundColor = [UIColor grayColor];
        [self.relevanceButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        self.relevanceButton.backgroundColor = [UIColor clearColor];
    }
    
    return topView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ALPS_TSearchAllDModel *model = [self.dataSource objectAtIndex:indexPath.row];
    ALPS_PlayerMainViewController *pmVC = [[ALPS_PlayerMainViewController alloc] init];
    pmVC.trackId = model.id;
    pmVC.listArr = self.idArray;
    [self.navigationController pushViewController:pmVC animated:YES];

}

#pragma mark -- 相关度点击方法
-(void)didClickedRelevanceButton:(UIButton *)sender
{
    [self.idArray removeAllObjects];
    [self.dataSource removeAllObjects];
    [self.tableView reloadData];
    [self getRelevanceData:self.searchText number:1];
    self.judge = 0;
    self.page = 1;
    
}
#pragma mark -- 播放最多点击方法
-(void)didClickedPlayButton:(UIButton *)sender
{
    [self.idArray removeAllObjects];
    [self.dataSource removeAllObjects];
    [self.tableView reloadData];
    [self getPlayData:self.searchText number:1];
    self.judge = 1;
    self.page = 1;
}
#pragma mark -- 最新上传点击方法
-(void)didClickedLatestUploadButton:(UIButton *)sender
{
    [self.idArray removeAllObjects];
    [self.dataSource removeAllObjects];
    [self.tableView reloadData];
    [self getLatestUploadData:self.searchText number:1];
    self.judge = 2;
    self.page = 1;
}


#pragma mark -- 键盘回收
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.value(1);
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
